<?php

namespace Biz\service\miniProgram;

use Biz\model\activity\RecommendGoods as mRecommendGoods;
use Doraemon\tools\ImgUrl;
use Biz\service\Goods;

class GoodsRecommend
{
    /**
     * @desc 商城小程序首页好物推荐
     */
    public static function main()
    {
        $carousel = [];
        $goodsList = [];
        $result = mRecommendGoods::getValidList(0, 5);
        if ($result) {
            $carousel = array_column($result, 'pic_id');
            $goodsIds = array_column($result, 'goods_id');
            //图片地址处理
            $carousel = array_map(function($v){
                return $v ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $v, ImgUrl::IMAGE_SIZE_OP_ORI) : '';
            }, $carousel);
            //获取商品信息
            $goodsInfo = Goods::Goods($goodsIds);
            foreach ($result as $key => $value) {
                $value['goodsInfo'] = isset($goodsInfo[$value['goods_id']]) ? $goodsInfo[$value['goods_id']] : [];
                $goodsList[] = $value;
            }
        }
        return compact('carousel', 'goodsList');
    }

    /**
     * @desc 商城小程序好物推荐列表
     * @param $page
     * @param int $size
     * @return array
     */
    public static function paged($page, $size = 10)
    {
        $page = max(1, $page);
        $offset = ($page - 1) * $size;
        $list = [];
        $result = mRecommendGoods::getValidList($offset, $size);
        if ($result) {
            $goodsIds = array_column($result, 'goods_id');
            //获取商品信息
            $goodsInfo = Goods::Goods($goodsIds);
            foreach ($result as $key => $value) {
                $value['picUrl'] = $value['pic_id'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $value['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI) : '';
                $value['goodsInfo'] = isset($goodsInfo[$value['goods_id']]) ? $goodsInfo[$value['goods_id']] : [];
                $list[] = $value;
            }
        }
        $count = mRecommendGoods::getValidCount();
        $hasNext = $count > ($offset + $size);
        return compact('list', 'hasNext');
    }

}